package mapper;

import model.Prescription;
import model.dto.PrescriptionDto;
import model.vo.PrescriptionVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface PrescriptionMapper {
    @Insert("insert into prescription (registry_id, patient_id, pay_status, fee, pay_fee, medicare_fee)" +
            "values (#{registryId},#{patientId},#{payStatus},#{fee},#{payFee},#{medicareFee})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    int add(Prescription prescription);

    @Select("<script>select p.*,p2.name,d.name dname,su.user_name doctor from prescription p\n" +
            "join patient p2 on p2.id = p.patient_id\n" +
            "join registry r on p.registry_id = r.id\n" +
            "join sys_users su on r.user_id = su.id\n" +
            "join department d on d.id = su.department_id" +
            " <where>" +
            "<if test=\"id>0\"> and p.id = #{id}</if>" +
            "<if test=\"name!=null and name!=''\"> and p2.name like \"%\"#{name}\"%\"</if>" +
            "<if test=\"start!=null\"> and p.created_time &gt;= #{start}</if>" +
            "<if test=\"end!=null\"> and p.created_time &lt;= #{end}</if>" +
            "</where> limit #{page},#{limit}</script>")
    List<PrescriptionVo> findAll(PrescriptionDto dto);

    @Select("<script>select count(p.id) from prescription p\n" +
            "join patient p2 on p2.id = p.patient_id\n" +
            " <where>" +
            "<if test=\"id>0\"> and p.id = #{id}</if>" +
            "<if test=\"name!=null and name!=''\"> and p2.name like \"%\"#{name}\"%\"</if>" +
            "<if test=\"start!=null\"> and p.created_time &gt;= #{start}</if>" +
            "<if test=\"end!=null\"> and p.created_time &lt;= #{end}</if>" +
            "</where> limit #{page},#{limit}</script>")
    int getCount(PrescriptionDto dto);

    @Update("update prescription set is_fetch_medicine=2 where id = #{id}")
    int updateIsFetchMedicine(Long id);
}
